﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;

namespace mamnontuoitho7.backend
{
    public partial class AlbumCreate : System.Web.UI.Page
    {
        Album _album = new Album();
        protected void Page_Load(object sender, EventArgs e)
        {
            //Check Role Admin
            if (Session["memberID"].ToString() != "" && (ConstantClass.permissionArticle).IndexOf(Session["role"].ToString() + ",") != -1)
            {
            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }
        }

        protected void btnAlbumCreate_Click(object sender, EventArgs e)
        {
            try
            {
                string FilePath = Request.PhysicalApplicationPath + "images/album";

                int currentAlbum = _album.AlbumCreateBySP("2", Session["memberID"].ToString(), txtTitleName.Text.Trim(), txtDescription.Text.Trim(), "0", "1", Session["username"].ToString(), "");
                if (currentAlbum != -99)
                {
                    String photoName = UploadImage(currentAlbum.ToString());

                    _album.UpdateAlbumThumbmail(photoName.Trim(), currentAlbum.ToString());
                    txtTitleName.Text = "";
                    txtDescription.Text = "";
                    lblNotice.Text = "Tạo Album thành công";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorAdminPage);
            }
        }

        private string UploadImage(String productID)
        {
            if (!string.IsNullOrEmpty(fulPhoto.FileName))
            {
                if (fulPhoto.FileBytes.Count() / 1024 > 6144)
                {
                    String script = "<script language=JavaScript>alert('Dung lượng hình ảnh phải dưới 6mb')</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                }
                string[] splitFileName = (fulPhoto.FileName.Split(new char[] { '.' }));
                string fileExtension = splitFileName[splitFileName.Length - 1].Trim().ToLower();
                if (fileExtension == "jpg" || fileExtension == "gif" || fileExtension == "jpeg" || fileExtension == "png" || fileExtension == "bmp" || fileExtension == "tiff")
                {
                    string FilePath = Request.PhysicalApplicationPath + "images/album/";
                    string code = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                    string name = txtTitleName.Text + "-" + DateTime.Now.Ticks;
                    ThumbnailClass thumb = new ThumbnailClass();
                    String filename = thumb.UploadAlbumTopicImage(fulPhoto, FilePath, code, name, productID);
                    return filename;
                }
                else
                {
                    String script = "<script language=JavaScript>alert('Chỉ hỗ trợ những file: jpg, jpeg, gif, png, bmp, tiff')</script>";
                    Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", script);
                }
            }

            return "nothing";
        }
    }
}